package person.zjh.kaoyansharehub.controller;

import cn.hutool.core.lang.UUID;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;
import person.zjh.kaoyansharehub.config.UploadFileConfig;
import person.zjh.kaoyansharehub.dto.MaterialSaveDTO;
import person.zjh.kaoyansharehub.dto.PageMaterialDTO;
import person.zjh.kaoyansharehub.result.PageResult;
import person.zjh.kaoyansharehub.result.Result;
import person.zjh.kaoyansharehub.service.MaterialService;
import person.zjh.kaoyansharehub.vo.MaterialVO;

import java.io.*;
import java.net.URLEncoder;
import java.util.List;


/**
 * @description: 学习资料控制层
 * @author 郑佳豪
 * @date 2025/2/13 16:23
 * @version 1.0
 */
@RestController
@RequestMapping("/front/material")
@Slf4j
@RequiredArgsConstructor
public class MaterialController {

    private final MaterialService materialService;


    @PostMapping("/saveMaterial")
    public Result<Void> saveMaterial(@ModelAttribute MaterialSaveDTO requestParam) {
        log.info("资料上传参数{}", requestParam);
        return materialService.saveMaterial(requestParam);
    }


    @GetMapping("/downloadFile")
    public Result downloadFile(HttpServletRequest request, HttpServletResponse response, String filePath, Long materialId, Long userId)  {
        // 清空输出流
        response.reset();
        log.info("下载文件路径:{}", filePath);
        log.info("下载用户id:{}", userId);
        log.info("下载文件id:{}", materialId);
        return materialService.downloadFile(request,response,filePath, materialId, userId);
    }

    @PostMapping("/pageMaterial")
    public Result<PageResult> pageMaterial(@RequestBody PageMaterialDTO requestParam) {
        log.info("资料分页组合查询参数{}", requestParam);
        return Result.success(materialService.pageMaterial(requestParam));
    }

    @GetMapping("/getMaterialInfo")
    public Result<MaterialVO> queryMaterial(Long materialId) {
        log.info("查询资料的id{}", materialId);
        return Result.success(materialService.queryMaterial(materialId));
    }


    @GetMapping("getNewMaterialList")
    public Result<List<MaterialVO>> getNewMaterialList(Long categoryId) {
        return Result.success(materialService.getNewMaterialList(categoryId));
    }

    @PostMapping("/getUserPublishArticle")
    public Result<PageResult> getUserPublishArticle(@RequestBody PageMaterialDTO requestParam) {
        log.info("获取用户的上传文章参数：{}", requestParam);
        return Result.success(materialService.getUserPublishArticle(requestParam));
    }


    @GetMapping("/getUserPublishMaterialCount")
    public Result getUserPublishMaterialCount(Long userId){
        return materialService.getUserPublishMaterialCount(userId);
    }



}



